package cn.wit.summit.chwan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.MakeMoneyRecommendAdapter;
import cn.wit.summit.chwan.models.response.MakeMoneyIndexResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.activity.TreviFountainListActivity;
import cn.wit.summit.chwan.ui.base.BaseFragment;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.NetWorkUtils;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.utils.ToastUtils;
import retrofit2.Retrofit;

/**
 * Created by Alan on 2018/7/25.
 */

public class MakeMoneyRecommendFragment extends BaseFragment {

    @Bind(R.id.makeMoneyIncomeTv)
    TextView makeMoneyIncomeTv;
    @Bind(R.id.makeMoneyHighestIncomeTv)
    TextView makeMoneyHighestIncomeTv;
    @Bind(R.id.makeMoneyPeopleTv)
    TextView makeMoneyPeopleTv;
    @Bind(R.id.makeMoneyHelpTv)
    TextView makeMoneyHelpTv;
    @Bind(R.id.makeMoneyTreviIv)
    SimpleDraweeView makeMoneyTreviIv;
    @Bind(R.id.makeMoneyCheckWalletTv)
    TextView makeMoneyCheckWalletTv;
    @Bind(R.id.makeMoneyGv)
    GridView makeMoneyGv;

    private List<MakeMoneyIndexResponse.MyInfoBean> funList = new ArrayList<>();
    private MakeMoneyRecommendAdapter adapter;

    private RetrofitManager retrofitManager;
    private Retrofit retrofitLogin;
    private ApiUtil apiLogin;

    private int statusId = 1;//0:正常、1：无数据、2：未登录、3：无网络

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_make_money_recommend, container, false);
        ButterKnife.bind(this, view);

        retrofitManager = RetrofitManager.getInstance(getActivity());
        retrofitLogin = retrofitManager.getLoginRetrofit();
        apiLogin = retrofitLogin.create(ApiUtil.class);

        initView();
        return view;
    }

    private void initView() {
        adapter = new MakeMoneyRecommendAdapter(getActivity(), funList);
        makeMoneyGv.setAdapter(adapter);
        makeMoneyGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.getInstance(getContext()).showToastSystem("" + (position + 1));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.e("tbl>>>Recommend>>onResume()");
        getMoneyRanking();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtils.e("tbl>>>Recommend>>setUserVisibleHint");
        if (getActivity() != null && isVisibleToUser && statusId !=0) {
            getMoneyRanking();
        }
    }

    private void getMoneyRanking() {
        statusId = 0;

        int uid = 0;
        if (AccountUtil.getInstance().getAccountData() != null) {
            uid = AccountUtil.getInstance().getUid();
        }
        if (NetWorkUtils.isNetworkConnected(getActivity())) {
            apiLogin.getMakeMoneyIndex(uid, AccountUtil.getInstance().getToken())
                    .enqueue(new MyCallback<ResponseBase<MakeMoneyIndexResponse>>() {
                        @Override
                        public void onSuccess(ResponseBase<MakeMoneyIndexResponse> model) {
                            MakeMoneyIndexResponse data = model.getData_info();
                            upUiView(data);
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            showToast(msg);
                            if (code==-3) {
                                changeStatusView(3);
                            } else {
                                changeStatusView(1);
                            }
                        }
                    });
        } else {
            changeStatusView(3);
        }
    }

    private MakeMoneyIndexResponse.RankInfoBean mRankInfoBean;
    private MakeMoneyIndexResponse.BannerInfoBean mBannerInfoBean;
    private void upUiView(MakeMoneyIndexResponse data) {
        mRankInfoBean = data.getRankInfo();
        if (mRankInfoBean!=null) {
            makeMoneyIncomeTv.setText(data.getRankInfo().getYesterdayIncome()+"");
            makeMoneyHighestIncomeTv.setText(data.getRankInfo().getMaxIncome()+"");
            makeMoneyPeopleTv.setText(data.getRankInfo().getPeopleCount()+"");
        }

        mBannerInfoBean = data.getBannerInfo();
        if (mBannerInfoBean!=null) {
            String url = data.getBannerInfo().getJumpCoverBigPic();
            if (!StringUtils.isEmpty(url)) {
                makeMoneyTreviIv.setImageURI(url);
            }
        }

        if (data.getMyInfo()!=null && data.getMyInfo().size()>0) {
            setFunData(data.getMyInfo());
        }
    }

    private void changeStatusView(int m) {
        statusId = m;
        if (statusId == 0) {
//            failedMessageTv.setVisibility(View.GONE);
//            makeMoneyIncomeRankRv.setVisibility(View.VISIBLE);
//            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 1) {
//            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nodata));
//            failedMessageTv.setVisibility(View.VISIBLE);
//            makeMoneyIncomeRankRv.setVisibility(View.GONE);
//            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 2) {
//            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nologin));
//            failedMessageTv.setVisibility(View.VISIBLE);
//            makeMoneyIncomeRankRv.setVisibility(View.GONE);
//            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 3) {
//            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nonet));
//            rootContentNullLl.setVisibility(View.VISIBLE);
//            failedMessageTv.setVisibility(View.GONE);
//            makeMoneyIncomeRankRv.setVisibility(View.GONE);
        }
    }

    private void setFunData(List<MakeMoneyIndexResponse.MyInfoBean> myInfo) {
        funList.clear();
        funList.addAll(myInfo);
        adapter.notifyDataSetChanged();
    }

    public static MakeMoneyRecommendFragment newInstance() {
        Bundle args = new Bundle();
        MakeMoneyRecommendFragment fragment = new MakeMoneyRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.makeMoneyHelpTv, R.id.makeMoneyTreviIv, R.id.makeMoneyCheckWalletTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.makeMoneyHelpTv:
                if (AccountUtil.getInstance().getUidNotToLogin(getActivity()) == 0) return;
                if (mRankInfoBean!=null) {
                    IntentUtil.getInstance().intentTo(getActivity(), mRankInfoBean.getIntroJumpInfo());
                }
                break;
            case R.id.makeMoneyTreviIv:
                startActivity(new Intent(getActivity(), TreviFountainListActivity.class));
//                if (mBannerInfoBean!=null) {
//                    IntentUtil.getInstance().intentTo(getActivity(), mBannerInfoBean.getJumpInfo());
//                }
//                IntentDataBean mIntentDataBean = new IntentDataBean();
//                mIntentDataBean.setJump_link_type(3);
//                mIntentDataBean.setJump_type(17);
//                mIntentDataBean.setJump_link_type_val("17");
//                IntentUtil.getInstance().intentTo(getActivity(), mIntentDataBean);
                break;
            case R.id.makeMoneyCheckWalletTv:
                if (AccountUtil.getInstance().getUidNotToLogin(getActivity()) == 0) return;
                IntentUtil.getInstance().goShareWebActivity(getActivity(), MyApplication.cfgPlug.getValue_wallet_url_v2());
                break;
        }
    }
}
